リクルートテクノロジーズ エンジニアブログ 用のサンプルスクリプト

http://blog.recruit-tech.co.jp/

検索チームのロゴ

\$ x_2 \$ と書くと$ x_2$となる

In [1]:
# -*- coding: utf_8 -*-  
%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn
from plotly import __version__
from plotly.offline import download_plotlyjs, init_notebook_mode, iplot
print __version__ # requires version >= 1.9.0
init_notebook_mode()
import plotly.plotly as py
import plotly.graph_objs as go
from sklearn.datasets import fetch_mldata
iris = fetch_mldata('iris')
1.9.2

?をつけて関数を実行するとヘルプが開きます

In [2]:
plt.hist?

お勧めはしませんがOSコマンドも実行できます

In [3]:
!ls
README.md
sample_of_visualizations_via_jupyter_notebook.ipynb
sample_of_visualizations_via_jupyter_notebook.ipynb のコピー
In [4]:
fileList = !ls
for f in fileList:
    print f
README.md
sample_of_visualizations_via_jupyter_notebook.ipynb
sample_of_visualizations_via_jupyter_notebook.ipynb のコピー

matplotlib (seaborn)のサンプル

In [5]:
x = iris.data[np.where(iris.target==1)][:, 0]
bins = np.array(range(-105, 0, 10))*0.1*0.1
plt.hist(x, bins, label='target:1')
plt.legend()
plt.title('iris visualization')
plt.xlabel('feature: 1')
plt.ylabel('Count')
Out[5]:
<matplotlib.text.Text at 0x109c97550>

細かい設定は大変ですが、手軽に図をつくるだけならpandasのDataframe型がおすすめ

In [6]:
x_pd = pd.DataFrame(data=np.array([iris.data[:,0],iris.target]).T, columns=['feature: 1', 'target'])
x_pd[x_pd.target==1].hist(column='feature: 1', bins=bins)
Out[6]:
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x109c0c750>]], dtype=object)

plotly のサンプル

https://plot.ly/python/histograms/ を参考 動的な描画になっているので、ポインティングしたら値がポップアップしたり、表示されるデータを変更したり、拡大縮小した後にpngでダウンロードもできます

In [7]:
x = iris.data[np.where(iris.target==1)][:,0]
hist_plotly = go.Histogram(x=x, name='target: 1', opacity=0.5,autobinx=False,
    xbins=dict(start=-1.05,end=0.05,size=0.1))
layout = go.Layout(
    title = 'iris visualization',
    barmode='overlay',
    xaxis=dict(
        title='feature:1',
        
    ),
    yaxis=dict(
        title='Count'
    )
)
fig = go.Figure(data=[hist_plotly], layout=layout)
iplot(fig)
Drawing...